package uml.objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Rick Myers
 * @author Sean McKay
 */
public class ArrowLine implements Translatable {

	private Color m_color;
	private List<AnchorPoint> m_anchorPoints;
	
	/**
	 * Creates a new ArrowLine object. The default is created with a color value of black.
	 */
	public ArrowLine()
	{
		m_color = new Color (0,0,0);
		m_anchorPoints = new LinkedList<AnchorPoint>();
	}
	
	/**
	 * This will make the <tt>ArrowLine</tt> object paint itself.
	 * @param g
	 * @param offset
	 */
	public void paint(Graphics g, Point offset)
	{
		int[] x = new int[m_anchorPoints.size()]; 
		int[] y = new int[m_anchorPoints.size()];
		int i = 0;
		for(AnchorPoint p : m_anchorPoints)
		{
			Point screenPoint = p.convertCoordsToContainer();
			x[i] = screenPoint.x - offset.x;
			y[i] = screenPoint.y - offset.y;
			++i;
		}
		g.drawPolyline(x, y, m_anchorPoints.size());
	}
	
	/**
	 * Sets ArrowLine to new color.
	 * @param c
	 */
	public void setColor(Color c)
	{
		m_color = c;
	}
	
	/**
	 * @return current color of ArrowLine object.
	 */
	public Color getColor()
	{
		return m_color;
	}
	
	@Override
	public boolean select(Point p) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void translate(int x, int y) 
	{
		// TODO Add translate functionality to ArrowLines
		// Most likely to be used with MultiSelect
	}
	
	/**
	 * This function is used to add <tt>AnchorPoint</tt> to our arrow line
	 * so that it knows where to anchor itself.
	 * @param p
	 */
	public void addPoint(AnchorPoint p)
	{
		m_anchorPoints.add(p);
	}
	
	/**
	 * This function is used to determine the angle at the end of a line.
	 * 
	 * @param m_front
	 * @return float
	 */
	public float getEndAngle(boolean m_front) 
	{
		Point p1;
		Point p2;
		if(m_front)
		{
			p1 = m_anchorPoints.get(0);
			p2 = m_anchorPoints.get(1);
		}
		else
		{
			p1 = m_anchorPoints.get(m_anchorPoints.size() - 2);
			p2 = m_anchorPoints.get(m_anchorPoints.size() - 1);
		}
		float a = p1.y - p2.y;
		float b = p1.x - p2.x;
		return (float) Math.atan(a/b);
	}
}
